import platform
import socket
import typing as tp

import psutil


def get_host_ip4s() -> tp.Dict[str, str]:
    """ip4->mac"""
    mapping = {}
    addrs = psutil.net_if_addrs()
    for interface, addr_list in addrs.items():
        ipv4 = None
        mac = None
        for addr in addr_list:
            if addr.family == socket.AF_INET:  # IPv4地址
                ipv4 = addr.address
            elif addr.family == socket.AF_PACKET:  # MAC地址
                mac = addr.address.replace('-', ':').lower()  # 统一格式
        if ipv4 and mac:
            mapping[ipv4] = mac
    return mapping


class AgentInfo(tp.TypedDict):
    hostname: str
    ip: str
    mac: str
    arch: str
    osVersion: str
    kernelVersion: str
    cpuType: str
    cpuNum: str
    memType: str
    memSize: int
    hddType: str
    hddNum: str
    hddSize: str
    netType: str
    vgaType: str
    commitVersion: str
    dockerStatus: int
    dockerImageNum: int
    iosType: str
    seriesType: str


def get_host_info() -> AgentInfo:
    sys_info = platform.uname()
    return AgentInfo(
        hostname=sys_info.node,
        ip="127.0.0.1",
        mac="00:00:00:00:00:00",
        arch=sys_info.machine,
        osVersion=sys_info.version,
        kernelVersion=sys_info.release,
        cpuType="",
        cpuNum=psutil.cpu_count(False) or psutil.cpu_count(),
        memType="",
        memSize=psutil.virtual_memory().total,
        hddType="",
        hddNum=str(0),
        hddSize=str(psutil.disk_usage("/").total / 1024 / 1024 / 1024)
    )

